package com.isunland.managesystem.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.EmployeeLoanContent;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import java.util.HashMap;
import org.json.JSONException;

public class StopTaskFragment extends Fragment {

  public static final String EXTRA_RUNID = "com.isunland.managesystem.ui.EXTRA_RUNID";

  private BaseVolleyActivity mActivity;
  private CurrentUser mCurrentUser;
  private EditText mEtStopcause;
  private String runId;
  private EmployeeLoanContent mEmployeeLoanContent;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle("终止原因");
    mActivity = (BaseVolleyActivity) getActivity();
    mCurrentUser = CurrentUser.newInstance(getActivity());
    if (NavUtils.getParentActivityIntent(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    //		runId=(String) getArguments().getSerializable(EXTRA_RUNID);
    mEmployeeLoanContent = (EmployeeLoanContent) getArguments().getSerializable(EXTRA_RUNID);
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityIntent(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      // 提交反馈意见
      case R.id.menu_item_confirm:

        if (TextUtils.isEmpty(mEtStopcause.getText().toString())) {
          Toast.makeText(getActivity(), R.string.cause, Toast.LENGTH_SHORT).show();
        } else {
          //终止流程
          VolleyStop();
        }

        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_stop_task, container, false);
    mEtStopcause = (EditText) v.findViewById(R.id.et_stop_cause);
    return v;
  }

  private void VolleyStop() {

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_STOP_TASK);
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("runId", mEmployeeLoanContent.getRunId().toString());
    params.put("id", mEmployeeLoanContent.getId().toString());
    params.put("memo", mEtStopcause.getText().toString());
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        MyUtils.dismissDialog();
        try {
          SuccessMessage[] result = new Gson().fromJson(arg0, SuccessMessage[].class);
          SuccessMessage workProcess = result[0];
          if (null == workProcess || null == workProcess.getResult()) {
            Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
            return;
          }
          String successType = workProcess.getResult();
          if (successType.equals(SuccessMessage.FAILURE)) {
            Toast.makeText(getActivity(), workProcess.getCause().toString(), Toast.LENGTH_SHORT)
                .show();
            return;
          }
          if (successType.equals(SuccessMessage.SUCCESS)) {
            Toast.makeText(getActivity(), R.string.success_operation, Toast.LENGTH_SHORT).show();
            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
          }
        } catch (Exception e) {
          e.printStackTrace();
          LogUtil.e("error", e);
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        // Auto-generated method stub

      }
    });
  }

  protected static Fragment newInstance(String runId) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_RUNID, runId);
    StopTaskFragment fragment = new StopTaskFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  protected static Fragment newInstance(EmployeeLoanContent mEmployeeLoanContent) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_RUNID, mEmployeeLoanContent);
    StopTaskFragment fragment = new StopTaskFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
